package com.dabai.qrtools.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.android.material.textfield.TextInputLayout;

/**
 * 视图 辅助工具类
 */
public class ViewUtils {


    /**
     * 解决滚动视图的冲突
     *
     * @param view 视图
     */
    @SuppressLint("ClickableViewAccessibility")
    public static void FixScrollViewConflict(@NonNull View view) {

        view.setOnTouchListener((v, event) -> {
            //canScrollVertically()方法为判断指定方向上是否可以滚动,参数为正数或负数,负数检查向上是否可以滚动,正数为检查向下是否可以滚动
            if (view.canScrollVertically(1) || view.canScrollVertically(-1)) {
                v.getParent().requestDisallowInterceptTouchEvent(true);//requestDisallowInterceptTouchEvent();要求父类布局不在拦截触摸事件
                if (event.getAction() == MotionEvent.ACTION_UP) { //判断是否松开
                    v.getParent().requestDisallowInterceptTouchEvent(false); //requestDisallowInterceptTouchEvent();让父类布局继续拦截触摸事件
                }
            }
            return false;
        });

    }

    /**
     * 设置视图的高度
     *
     * @param view 视图
     * @param px   px
     */
    public static void setViewHeight(View view, int px) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        linearParams.height = px;
        view.setLayoutParams(linearParams);
    }

    /**
     * 设置视图的宽度
     *
     * @param view 视图
     * @param px   px
     */
    public static void setViewWidth(View view, int px) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        linearParams.width = px;
        view.setLayoutParams(linearParams);
    }


    /**
     * 解析布局
     *
     * @param context 上下文
     * @param layout  布局
     * @return {@link View}
     */
    public static View inflateLayout(Context context, int layout) {
        return LayoutInflater.from(context).inflate(layout, null);
    }


    public static String getTextInputLayoutText(@NonNull TextInputLayout textInputLayout) {
        return textInputLayout.getEditText().getText().toString();
    }

    public static void setTextInputLayoutText(@NonNull TextInputLayout textInputLayout, String text) {
        textInputLayout.getEditText().setText(text);
    }

    /**
     * 文本视图println
     *
     * @param textView 文本视图
     * @param text     文本
     */
    @SuppressLint("SetTextI18n")
    public static void textViewPrintln(TextView textView, String text) {
        String str1 = textView.getText().toString();

        if (str1.isEmpty()) {
            textView.setText(str1);
        } else {
            textView.setText(str1 + "\n" + text);
        }
    }


    /**
     * 根据view获取bitmap
     *
     * @param view 视图
     * @return {@link Bitmap}
     */
    public static Bitmap getBitmapByView(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }


    /**
     * Drawable 转换为 Bitmap
     *
     * @param drawable 可拉的
     * @return {@link Bitmap}
     */
    public static Bitmap drawableToBitmap(Drawable drawable) {

        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }


}
